﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32;
namespace Congducsan
{
    public partial class FrmBaccapTc : Form
    {
        public FrmBaccapTc()
        {
            InitializeComponent();
        }

        private void tabControl2_Click(object sender, EventArgs e)
        {
            if (tabControl2.SelectedTab==tabControl2.TabPages[0])
            {
                pictureBox1.Image = Congducsan.Properties.Resources.Tcbcdau;
            }
            else
            {
                if (tabControl2.SelectedTab==tabControl2.TabPages[1])
                {
                    pictureBox1.Image = Congducsan.Properties.Resources.Tcbccanh;
                }
                else
                {
                    pictureBox1.Image = Congducsan.Properties.Resources.Tcbcsan;
                }
            }
        }

        private void FrmBaccapTc_FormClosing(object sender, FormClosingEventArgs e)
        {
            DradApplication.DradApp.FormMain.Show();
        }

        private void FrmBaccapTc_Resize(object sender, EventArgs e)
        {
            this.Size = new Size(652, 540);
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CmdOk_Click(object sender, EventArgs e)
        {
            PrBnTc prBnTc = new PrBnTc();
            prBnTc.Show();
            this.Hide();
        }

        private void FrmBaccapTc_Load(object sender, EventArgs e)
        {
            loadSettingReg();
        }
        private void saveSettingReg()
        {
            RegistryKey regKey = Registry.CurrentUser;
            regKey = regKey.CreateSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
            string Values = "";
            //Tuong chan
            Values = txtCaotuongchanH.Text; //0
            Values = Values + "\t" + txtBotriXamu.Checked.ToString(); //1
            Values = Values + "\t" + txtXamub1.Text; //2
            Values = Values +"\t"+ txtXamuh1.Text; //3
            Values = Values +"\t"+ txtVatlungv1.Text; //4
            Values = Values +"\t"+ txtVatlungv2.Text; //5
            Values = Values +"\t"+ txtDaymongtruocH1.Text; //6
            Values = Values +"\t"+ txtDaymongsauH2.Text; //7
            Values = Values +"\t"+ txtMomongtruocb2.Text; //8
            Values = Values +"\t"+ txtMomongsaub3.Text; //9
            Values = Values +"\t"+ txtRongmongb.Text; //10
            Values = Values +"\t"+ txtNghiengtuongchani.Text; //11
            Values = Values +"\t"+ txtDaituongchanL.Text; //12
            Values = Values +"\t"+ txtKcOngcongd1.Text; //13
            Values = Values +"\t"+ txtKcBaccapd2.Text; //14
            //Values = Values +"\t"+ "" //15
            //Bac nuoc
            Values = Values +"\t"+ txtBn_DayDinh.Text; //16
            Values = Values +"\t"+ txtBn_Day.Text; //17
            Values = Values +"\t"+ txtLopdem.Text; //18
            Values = Values +"\t"+ txtHtn_DayDra.Text; //19
            Values = Values +"\t"+ txtHtn_DayDvao.Text; //20
            Values = Values +"\t"+ txtHtn_DayMong.Text; //21
            Values = Values +"\t"+ txtHtn_Rong.Text; //22
            //Tcanh co
            Values = Values +"\t"+ txtHtn_SauDra.Text; //23
            Values = Values +"\t"+ txtHtn_SauDvao.Text; //24
            Values = Values +"\t"+ txtTCanh_CaoCuoi.Text; //25
            Values = Values +"\t"+ txtTCanh_CaoGiua.Text; //26
            Values = Values +"\t"+ txtTCanh_CaoChan.Text; //27
            Values = Values +"\t"+ txtTCanh_Day.Text; //28
            Values = Values +"\t"+ txtTCanh_Morong.Text; //29
            Values = Values +"\t"+ txtTCanh_MorongM.Text; //30
            Values = Values +"\t"+ txtTCanh_DocN.Text; //31
            Values = Values +"\t"+ txtBn_GocMoTren.Text; //32

            Values = Values + "\t" + txtRonggotieunang.Text; //0
            Values = Values +"\t"+ txtCaogotieunang.Text; //1
            Values = Values +"\t"+ txtDaigotieunang.Text; //2
            Values = Values +"\t"+ txtCkgotieunang.Text; //3
            Values = Values +"\t"+ txtRongroda.Text; //4
            Values = Values +"\t"+ txtCaoroda.Text; //5
            Values = Values +"\t"+ txtDairoda.Text; //6
            Values = Values +"\t"+ ChcBtn.Checked.ToString(); //7
            Values = Values +"\t"+ ChcGtn.Checked.ToString(); //8
            Values = Values +"\t"+ ChcRd.Checked.ToString(); //9
            regKey.SetValue("DimBaccapTc", Values);
            // đóng registry
            regKey.Close();
           
        }

        private void loadSettingReg()
        {
            RegistryKey regKey = Registry.CurrentUser;
            RegistryKey testKey = regKey.OpenSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
            if (testKey != null)
            {
                regKey = regKey.CreateSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
                string str = regKey.GetValue("DimBaccapTc").ToString();
                string[] STR = str.Split('\t');
                int i = 0;
                //Tuong chan
                txtCaotuongchanH.Text=STR[i++]; //0
                txtBotriXamu.Checked=bool.Parse(STR[i++]); //1
                txtXamub1.Text = STR[i++]; //2
                txtXamuh1.Text = STR[i++]; //3
                txtVatlungv1.Text = STR[i++]; //4
                txtVatlungv2.Text = STR[i++]; //5
                txtDaymongtruocH1.Text = STR[i++]; //6
                txtDaymongsauH2.Text = STR[i++]; //7
                txtMomongtruocb2.Text = STR[i++]; //8
                txtMomongsaub3.Text = STR[i++]; //9
                txtRongmongb.Text = STR[i++]; //10
                txtNghiengtuongchani.Text = STR[i++]; //11
                txtDaituongchanL.Text = STR[i++]; //12
                txtKcOngcongd1.Text = STR[i++]; //13
                txtKcBaccapd2.Text = STR[i++]; //14
                //Bac nuoc
                txtBn_DayDinh.Text = STR[i++]; //16
                txtBn_Day.Text = STR[i++]; //17
                txtLopdem.Text = STR[i++]; //18
                txtHtn_DayDra.Text = STR[i++]; //19
                txtHtn_DayDvao.Text = STR[i++]; //20
                txtHtn_DayMong.Text = STR[i++]; //21
                txtHtn_Rong.Text = STR[i++]; //22
                //Tcanh co
                txtHtn_SauDra.Text = STR[i++]; //23
                txtHtn_SauDvao.Text = STR[i++]; //24
                txtTCanh_CaoCuoi.Text = STR[i++]; //25
                txtTCanh_CaoGiua.Text = STR[i++]; //26
                txtTCanh_CaoChan.Text = STR[i++]; //27
                txtTCanh_Day.Text = STR[i++]; //28
                txtTCanh_Morong.Text = STR[i++]; //29
                txtTCanh_MorongM.Text = STR[i++]; //30
                txtTCanh_DocN.Text = STR[i++]; //31
                txtBn_GocMoTren.Text = STR[i++]; //32

                txtRonggotieunang.Text = STR[i++]; //0
                txtCaogotieunang.Text = STR[i++]; //1
                txtDaigotieunang.Text = STR[i++]; //2
                txtCkgotieunang.Text = STR[i++]; //3
                txtRongroda.Text = STR[i++]; //4
                txtCaoroda.Text = STR[i++]; //5
                txtDairoda.Text = STR[i++]; //6
                ChcBtn.Checked = bool.Parse(STR[i++]); //7
                ChcGtn.Checked = bool.Parse(STR[i++]); //8
                ChcRd.Checked = bool.Parse(STR[i++]); //9
                regKey.Close();
            }
        }

        private void saveSettingFile()
        {
            SaveFileDialog svDialog = new SaveFileDialog();
            svDialog.Filter = "Data files (*.tcb)|*.tcb";
            svDialog.InitialDirectory = "C://";
            svDialog.RestoreDirectory = true;
            svDialog.DefaultExt = "bac";
            string filename = "";
            if (svDialog.ShowDialog() == DialogResult.OK)
            {
                filename = svDialog.FileName;
                string Str;
                    //Tuong chan
                Str = "------TUONG CHAN------" + "\r\n";
                Str = Str + "Cao tuong chan:" + "\t" + txtCaotuongchanH.Text  +"\r\n" ;
                Str = Str + "Bo tri xa mu:" + "\t" + txtBotriXamu.Text  +"\r\n" ;
                Str = Str + "Rong xa mu:" + "\t" + txtXamub1.Text  +"\r\n" ;
                Str = Str + "Cao xa mu:" + "\t" + txtXamuh1.Text  +"\r\n" ;
                Str = Str + "Cao vat lung:" + "\t" + txtVatlungv1.Text  +"\r\n" ;
                Str = Str + "Rong vat lung:" + "\t" + txtVatlungv2.Text  +"\r\n" ;
                Str = Str + "Day mong truoc:" + "\t" + txtDaymongtruocH1.Text  +"\r\n" ;
                Str = Str + "Day mong sau:" + "\t" + txtDaymongsauH2.Text  +"\r\n" ;
                Str = Str + "Nghieng lung tuong chan:" + "\t" + txtMomongtruocb2.Text  +"\r\n" ;
                Str = Str + "Nghieng lung tuong chan:" + "\t" + txtMomongsaub3.Text  +"\r\n" ;
                Str = Str + "Rong mong tuong chan:" + "\t" + txtRongmongb.Text  +"\r\n" ;
                Str = Str + "Nghieng lung tuong chan:" + "\t" + txtNghiengtuongchani.Text  +"\r\n" ;
                Str = Str + "Dai tuong chan:" + "\t" + txtDaituongchanL.Text  +"\r\n" ;
                Str = Str + "Khoang cach dinh cap - cua ra:" + "\t" + txtKcOngcongd1.Text  +"\r\n" ;
                Str = Str + "Khoang cach dinh mong - dinh cap:" + "\t" + txtKcBaccapd2.Text  +"\r\n" ;
                Str = Str + "Lop dem mong:" + "\t" + txtLopdem.Text  +"\r\n" ;
                //Bac cap
                Str = Str + "------BAC CAP------"  +"\r\n" ;
                Str = Str + "Cao tuong H1:" + "\t" + txtTCanh_CaoDinh.Text  +"\r\n" ;
                Str = Str + "Cao tuong H2:" + "\t" + txtTCanh_CaoGiua.Text  +"\r\n" ;
                Str = Str + "Cao tuong H3:" + "\t" + txtTCanh_CaoChan.Text  +"\r\n" ;
                Str = Str + "Day bac H4:" + "\t" + txtBn_DayDinh.Text  +"\r\n" ;
                Str = Str + "Day bac H5:" + "\t" + txtBn_Day.Text  +"\r\n" ;
                Str = Str + "Chan khay b:" + "\t" + txtHtn_DayDvao.Text  +"\r\n" ;
                Str = Str + "Chan khay H:" + "\t" + txtHtn_SauDvao.Text  +"\r\n" ;
                Str = Str + "Rong b1:" + "\t" + txtTCanh_Morong.Text  +"\r\n" ;
                Str = Str + "Rong b2:" + "\t" + txtBn_Rong.Text  +"\r\n" ;
                Str = Str + "Day b3:" + "\t" + txtTCanh_Day.Text  +"\r\n" ;
                Str = Str + "Day b4:" + "\t" + txtTCanh_MorongM.Text  +"\r\n" ;
                Str = Str + "Goc mo tren:" + "\t" + txtBn_GocMoTren.Text  +"\r\n" ;
                Str = Str + "Goc mo duoi:" + "\t" + txtBn_GocMoDuoi.Text  +"\r\n" ;
                Str = Str + "Chieu dai mo goc:" + "\t" + txtTcanh_Daimogoc.Text  +"\r\n" ;
                Str = Str + "Nghieng tuong canh:" + "\t" + txtTCanh_DocN.Text  +"\r\n" ;
                Str = Str + "Rong cua vao B:" + "\t" + txtRongB.Text  +"\r\n" ;
                //Tieu nang
                Str = Str + "------TIEU NANG------"  +"\r\n" ;
                Str = Str + "Co be tieu nang:" + "\t" + ChcBtn.Checked.ToString()  +"\r\n" ;
                Str = Str + "Cao thanh H1:" + "\t" + txtHtn_SauDra.Text  +"\r\n" ;
                Str = Str + "Cao thanh H2:" + "\t" + txtTCanh_CaoCuoi.Text  +"\r\n" ;
                Str = Str + "Day long H3:" + "\t" + txtHtn_DayMong.Text  +"\r\n" ;
                Str = Str + "Dai b1:" + "\t" + txtHtn_Rong.Text  +"\r\n" ;
                Str = Str + "Day b2:" + "\t" + txtHtn_DayDra.Text  +"\r\n" ;
                Str = Str + "Co go tieu nang?:" + "\t" + ChcGtn.Checked.ToString()  +"\r\n" ;
                Str = Str + "Rong go:" + "\t" + txtRonggotieunang.Text  +"\r\n" ;
                Str = Str + "Cao go:" + "\t" + txtCaogotieunang.Text  +"\r\n" ;
                Str = Str + "Dai go:" + "\t" + txtDaigotieunang.Text  +"\r\n" ;
                Str = Str + "Cach khoang go:" + "\t" + txtCkgotieunang.Text  +"\r\n" ;
                Str = Str + "Co ro da?:" + "\t" + ChcRd.Checked.ToString()  +"\r\n" ;
                Str = Str + "Rong ro da:" + "\t" + txtRongroda.Text  +"\r\n" ;
                Str = Str + "Cao ro da:" + "\t" + txtCaoroda.Text  +"\r\n" ;
                Str = Str + "Dai ro da:" + "\t" + txtDairoda.Text  +"\r\n" ;
                Str = Str + "So hang:" + "\t" + txtHangRoda.Text  +"\r\n" ;

                //Ghi file dữ liệu
                FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(Str);
                sw.Close();
                fs.Close();
                MessageBox.Show("OK", "Thông báo");
            }
        }
        // load setting từ file vào form
        private void loadSettingFile()
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.Filter = "Data files (*.tcb)|*.tcb";
            openDialog.InitialDirectory = @"C:\";
            openDialog.FilterIndex = 2;
            openDialog.RestoreDirectory = true;
            openDialog.Multiselect = false;
            string filename;
            if (openDialog.ShowDialog() == DialogResult.OK)
            {
                filename = openDialog.FileName;
                FileStream fs = new FileStream(filename, FileMode.Open);
                StreamReader sr = new StreamReader(fs);
                string str = sr.ReadToEnd();
                fs.Close();
                fs.Close();
                string[] Values = str.Split('\n');
                int i = 1;
                txtCaotuongchanH.Text = Values[i].Split('\t')[1]; i++;
                txtBotriXamu.Text = Values[i].Split('\t')[1]; i++;
                txtXamub1.Text = Values[i].Split('\t')[1]; i++;
                txtXamuh1.Text = Values[i].Split('\t')[1]; i++;
                txtVatlungv1.Text = Values[i].Split('\t')[1]; i++;
                txtVatlungv2.Text = Values[i].Split('\t')[1]; i++;
                txtDaymongtruocH1.Text = Values[i].Split('\t')[1]; i++;
                txtDaymongsauH2.Text = Values[i].Split('\t')[1]; i++;
                txtMomongtruocb2.Text = Values[i].Split('\t')[1]; i++;
                txtMomongsaub3.Text = Values[i].Split('\t')[1]; i++;
                txtRongmongb.Text = Values[i].Split('\t')[1]; i++;
                txtNghiengtuongchani.Text = Values[i].Split('\t')[1]; i++;
                txtDaituongchanL.Text = Values[i].Split('\t')[1]; i++;
                txtKcOngcongd1.Text = Values[i].Split('\t')[1]; i++;
                txtKcBaccapd2.Text = Values[i].Split('\t')[1]; i++;
                txtLopdem.Text = Values[i].Split('\t')[1]; i++;
                //Bac cap
                i = i + 1;
                txtTCanh_CaoDinh.Text = Values[i].Split('\t')[1]; i++;
                txtTCanh_CaoGiua.Text = Values[i].Split('\t')[1]; i++;
                txtTCanh_CaoChan.Text = Values[i].Split('\t')[1]; i++;
                txtBn_DayDinh.Text = Values[i].Split('\t')[1]; i++;
                txtBn_Day.Text = Values[i].Split('\t')[1]; i++;
                txtHtn_DayDvao.Text = Values[i].Split('\t')[1]; i++;
                txtHtn_SauDvao.Text = Values[i].Split('\t')[1]; i++;
                txtTCanh_Morong.Text = Values[i].Split('\t')[1]; i++;
                txtBn_Rong.Text = Values[i].Split('\t')[1]; i++;
                txtTCanh_Day.Text = Values[i].Split('\t')[1]; i++;
                txtTCanh_MorongM.Text = Values[i].Split('\t')[1]; i++;
                txtBn_GocMoTren.Text = Values[i].Split('\t')[1]; i++;
                txtBn_GocMoDuoi.Text = Values[i].Split('\t')[1]; i++;
                txtTcanh_Daimogoc.Text = Values[i].Split('\t')[1]; i++;
                txtTCanh_DocN.Text = Values[i].Split('\t')[1]; i++;
                txtRongB.Text = Values[i].Split('\t')[1]; i++;
                //Tieu nang
                i = i + 1;
                ChcBtn.Checked = bool.Parse(Values[i].Split('\t')[1]); i++;
                txtHtn_SauDra.Text = Values[i].Split('\t')[1]; i++;
                txtTCanh_CaoCuoi.Text = Values[i].Split('\t')[1]; i++;
                txtHtn_DayMong.Text = Values[i].Split('\t')[1]; i++;
                txtHtn_Rong.Text = Values[i].Split('\t')[1]; i++;
                txtHtn_DayDra.Text = Values[i].Split('\t')[1]; i++;
                ChcGtn.Checked = bool.Parse(Values[i].Split('\t')[1]); i++;
                txtRonggotieunang.Text = Values[i].Split('\t')[1]; i++;
                txtCaogotieunang.Text = Values[i].Split('\t')[1]; i++;
                txtDaigotieunang.Text = Values[i].Split('\t')[1]; i++;
                txtCkgotieunang.Text = Values[i].Split('\t')[1]; i++;
                ChcRd.Checked = bool.Parse(Values[i].Split('\t')[1]); i++;
                txtRongroda.Text = Values[i].Split('\t')[1]; i++;
                txtCaoroda.Text = Values[i].Split('\t')[1]; i++;
                txtDairoda.Text = Values[i].Split('\t')[1]; i++;
                txtHangRoda.Text = Values[i].Split('\t')[1]; i++;
                MessageBox.Show("OK", "Thông báo");
            }
        }
        private void cmdThietlap_Click(object sender, EventArgs e)
        {
            FrmTuychon frmTuychon = new FrmTuychon();
            frmTuychon.Show();
        }

        private void SaveData_Click(object sender, EventArgs e)
        {
            saveSettingFile();
        }

        private void LoadData_Click(object sender, EventArgs e)
        {
            loadSettingFile();
        }

        private void FrmBaccapTc_FormClosed(object sender, FormClosedEventArgs e)
        {
            saveSettingReg();
        }
    }
}
